﻿using MonitorPlatform.Common;
using MonitorPlatform.DataAccess;
using MonitorPlatform.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MonitorPlatform.ViewModel
{
    public class LoginViewModel:NotifyBase
    {
        public LoginModel LoginModel { get; set; }=new LoginModel();
        public CommandBase CloseWindowCommand { get; set; }
        public CommandBase LoginCommand { get; set; }
        private string _errorMessage;

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; this.DoNotify(); }
        }
        private Visibility _showProgress = Visibility.Collapsed;

        public Visibility ShowProgress
        {
            get { return _showProgress; }
            set { _showProgress = value; this.DoNotify(); LoginCommand.RaiseCanExecuteChanged(); }
        }
        public LoginViewModel()
        {
            //this.LoginModel = new LoginModel();
            //this.LoginModel.UserName = "Jovan";
            //this.LoginModel.Password = "123456";

            this.CloseWindowCommand = new CommandBase();
            this.CloseWindowCommand.DoExecute = new Action<object>((o) =>
            {
                (o as Window).Close();
            });
            this.CloseWindowCommand.DoCanExecute = new Func<object, bool>((o) =>
            {
                return true;
            });

            this.LoginCommand = new CommandBase();
            this.LoginCommand.DoExecute = new Action<object>( DoLogin );
            this.LoginCommand.DoCanExecute = new Func<object, bool>((o) =>
            {
                return ShowProgress == Visibility.Collapsed; ;
            });
        }
        private void DoLogin(object o)
        {
            this.ShowProgress = Visibility.Visible;
            this.ErrorMessage = "";
            if (string.IsNullOrEmpty(LoginModel.UserName))
            {
                this.ErrorMessage = "请输入用户名！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (string.IsNullOrEmpty(LoginModel.Password))
            {
                this.ErrorMessage = "请输入密码！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            Task.Run(new Action(async () =>
            {
                await Task.Delay(1000);
                try
                {
                    //var user = LocalDataAccess.GetInstance().CheckUserInfo(LoginModel.UserName, LoginModel.Password);
                    if (LoginModel.UserName == null|| LoginModel.UserName!="admin"|| LoginModel.Password==null|| LoginModel.Password!="123456")
                    {
                        throw new Exception("登陆失败！用户名或密码错误！");
                    }
                    //GlobalValues.UserInfo = user;

                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        (o as Window).DialogResult = true;
                    }));
                }
                catch (Exception ex)
                {
                    this.ErrorMessage = ex.Message;


                }
                finally
                {
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        this.ShowProgress = Visibility.Collapsed;
                    }));
                }
            }));
        }
    }
}
